<?php
/**
 * Created by PhpStorm.
 * User: DaveTang
 * Date: 2017/12/18
 * Time: 15:56
 */

namespace App\Controller\Component;
use Cake\Controller\Component;

class AdminCheckComponent extends Component
{
    public $components = ['Jwt'];

    protected $_defaultConfig = [
        'checkedActions' => '*'
    ];

    public function initialize(array $config)
    {
        parent::initialize($config);
    }

    /**
     * 管理功能权限判断
     *
     * @return True | Error Msg
     *
     */
    public function check()
    {
        $checkedActions = $this->getConfig('checkedActions');

        if (!is_array($checkedActions) && $checkedActions === '*')
        {
            return true;
        }

        $controller = $this->_registry->getController();
        $ctr = $controller->request->getParam('controller');
        $act = $controller->request->getParam('action');

        if (array_key_exists($ctr, $checkedActions))
        {
            if (array_search($act, $checkedActions[$ctr]) !== false)
            {
                $user_type = $this->Jwt->get('user_type');
                if($user_type == 'admin'){
                    return true;
                }else{
                    return false;
                }
            }
        }

        return true;
    }

}